/**
 * Create by yz_wy_power on 2018/7/2
 */

/**
 * 所有新闻列表数据
 */
const Mock = require('mockjs')
const Random = Mock.Random

const getNewsList = (req) => {
  const {page = 1, pageSize = 10, type = '', status = '', order = ''} = JSON.parse(req.body)
  const reqData = {page, pageSize, type, status, order}
  let newLists = Mock.mock({
    'data': {
      'rows|100': [{
        'id|+1': 1,
        'title': Random.ctitle(),
        'type|1': ['普通', '图文', '焦点', '头条'],
        'weight|1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        'author|1': ['Admin', 'Guest', 'Jack', 'Lily'],
        'clickTime|1-20000': 1,
        'publishTime|1420070402000-1530523397673': 1,
        'status|1': ['待审', '审核通过', '草稿', '退回'],
        'actionStatus|1': [true, false]
      }],
      'totalRecords': 100
    },
    'error_code': 0,
    'error_msg': 'success'
  })
  let handledResult = handleRes(newLists.data.rows, reqData)
  newLists.data.rows = handledResult['returnResult']
  newLists.data.totalRecords = handledResult['selectedResult'].length
  return newLists
}

function handleRes(data = [], {page, pageSize, type, status, order}) {
  let selectedResult = data.filter(x => {
    return (!type || (x.type === type)) && (!status || (x.status === status)) && (!order || (x.order === order))
  })
  return {
    selectedResult,
    returnResult: selectedResult.slice((page - 1) * pageSize, page * pageSize)
  }
}

export default getNewsList
